package com.FH.applyFrom.expen_form;

import com.FH.applyForm.officeServies.expenform_firsttype.ExpenformResource;
import com.FH.applyForm.officeServies.expenform_name.ExpenformnamePullDTO;
import com.FH.applyForm.officeServies.expenform_name.ExpenformnameResource;
import com.FH.organization.Attachment.AttachmentResource;
import com.FH.organization.operator_log.OperatorLogPullDTO;
import com.FH.organization.operator_log.OperatorLogResource;
import com.FH.organization.people.KamMemberResource;
import com.FH.organization.suggestion.SuggestionResource;
import com.FH.organization.work_flow.WorkFlowPullDTO;
import com.FH.organization.work_flow.WorkFlowResource;
import com.FH.util.AccessDB;
import com.FH.util.FileProcess;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/7/13.
 */
@Stateless
public class ExpenFormDAOImpl extends AccessDB implements ExpenFormDAO {


    @Inject
    OperatorLogResource operatorLogResource;

    @Inject
    WorkFlowResource workFlowResource;
    @Inject
    ExpenFormResource expenFormResource;

    @Inject
    ExpenformnameResource expenformnameResource;

    @Inject
    AttachmentResource attachmentResource;

    @Inject
    ExpenformResource expenformfirstResource;
    @Inject
    KamMemberResource kamMemberResource;
    @Inject
    SuggestionResource suggestionResource;

    @Override
    public ResultDTO getExpenformInfo(String member_id, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(member_id);
            String existName = "0";

            //先查询数据库中是否存在字段
            String sqlexit = "select count(2) from expen_form  where 1=1 ";

            String sql_w = "";//查询条件sql组合
            String sql_page = "";//分页组合

            Integer page_ok = Integer.parseInt(page);//将定义page、size的String类型转换成Integer类型
            Integer size_ok = Integer.parseInt(size);

            if (page.equals("0")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json.has("isverify")) {
                sql_w += " AND isverify ='" + json.getString("isverify") + "'";
            }
//            else {
//                sql_w += " AND (isverify IS NULL OR isverify=0)";
//            }


            if (json.has("member_id")) {
                sql_w += " AND member_id ='" + json.getString("member_id") + "'";
            }

            if (json.has("expen_form_id")) {
                sql_w += " AND expen_form_id ='" + json.getString("expen_form_id") + "'";
            }

            String sql_ok = sqlexit + sql_w + sql_page;//组合好的sql语句
            existName = getReportingToOne(sqlexit + sql_w);

            List<String[]> seresult = new ArrayList<String[]>();

            List<ExpenFormShowDTO> result = new ArrayList<ExpenFormShowDTO>();

            if (!existName.equals("0")) {
                String sql = "SELECT expen_form_id,expen_form_number,expen_form_type,expen_form_desc,expen_form_bignumber,expen_form_date,member_id,fail_resource,attachment_id,expen_form_name,expen_from_firsttype_id,form_code_number,form_code,isverify FROM expen_form where 1=1" + sql_w + " ORDER BY expen_form_date DESC" + sql_page;
                //     String sql ="select * from finance_budget" ;
                seresult = this.getReportingTo(sql);

                for (int i = 0; i < ((List) seresult).size(); ++i) {
                    ExpenFormShowDTO edo = new ExpenFormShowDTO();
                    edo.setExpen_form_id(((String[]) ((List) seresult).get(i))[1]);
                    edo.setExpen_form_number(((String[]) ((List) seresult).get(i))[2]);
                    edo.setExpen_form_type(((String[]) ((List) seresult).get(i))[3]);
                    edo.setExpen_form_desc(((String[]) ((List) seresult).get(i))[4]);
                    edo.setExpen_form_bignumber(((String[]) ((List) seresult).get(i))[5]);
                    edo.setExpen_form_date(((String[]) ((List) seresult).get(i))[6]);
                    edo.setMember_id(((String[]) ((List) seresult).get(i))[7]);
                    edo.setFail_resource(((String[]) ((List) seresult).get(i))[8]);
                    edo.setAttachment_id(((String[]) ((List) seresult).get(i))[9]);
                    edo.setExpen_form_name(((String[]) ((List) seresult).get(i))[10]);
                    edo.setExpen_from_firsttype_id(((String[]) ((List) seresult).get(i))[11]);
                    edo.setForm_code_number(((String[]) ((List) seresult).get(i))[12]);
                    edo.setForm_code(((String[]) ((List) seresult).get(i))[13]);
                    edo.setIsverify(((String[]) ((List) seresult).get(i))[14]);
                    ResultDTO worklist = null, userlist = null, attach = null, firsttype = null,meninfo=null,suggestion=null;
                    if (json.has("isverify")) {
                        String state = json.getString("isverify");
                        worklist = workFlowResource.getWorkFlowInfo("{work_flow_formid:" + seresult.get(i)[1] + ",work_flow_state:" + state + "}", "1", "20");
                    } else {
                        worklist = workFlowResource.getWorkFlowInfo("{work_flow_formid:" + seresult.get(i)[1] + "}", "1", "20");
                    }
                    edo.setWs(worklist.getValues());
                    if (seresult.get(i)[9] != null && !seresult.get(i)[9].equals("")) {
                        attach = attachmentResource.getAttachmentInfo("{attachment_id:" + seresult.get(i)[9] + "}", "1", "1");
                        edo.setAttachmentShowDTOs(attach.getValues());

                    }


                    //查询报销单多类别信息
                    if (seresult.get(i)[1] != null && !seresult.get(i)[1].equals("")) {
                        userlist = expenformnameResource.getExpenformNameInfo("{expen_form_id:" + seresult.get(i)[1] + "}", "1", "20");
                        edo.setQq(userlist.getValues());

                    }
                    if (seresult.get(i)[11] != null && !seresult.get(i)[11].equals("")) {

                        firsttype = expenformfirstResource.getExpenformTypeInfo("{advancetype_id:" + seresult.get(i)[11] + "}", "1", "1");
                        edo.setExpenformShowDTOList(firsttype.getValues());
                    }

                    meninfo=kamMemberResource.getUserListInfo("{member_id:" + seresult.get(i)[7] + "}", "1", "1");
                    edo.setMeminfo(meninfo.getValues());

                    if(seresult.get(i)[1] != null && ! seresult.get(i)[1].equals("")){

                        suggestion=suggestionResource.getSuggestionInfo("{form_id:" + seresult.get(i)[1] + "}", "1", "100");
                        edo.setSuggestionShowDTOs(suggestion.getValues());
                    }

                    result.add(edo);
                }
            }
            if (seresult.size() != 0) {

                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setStr(result.size() + "");
            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();

        }
        return resultDTO;
    }

    @Override
    public ResultDTO maxExpenformInfo() {
        ResultDTO resultDTO = new ResultDTO();
        String sql = "SELECT MAX(form_code_number) FROM expen_form";
        String result = this.getReportingToOne(sql);
        if (result != null && !result.equals("null")) {
            resultDTO.setStr(result);
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.get_success);
        } else {
            resultDTO.setStr("0");
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.get_fail);
        }
        return resultDTO;
    }


//        ResultDTO resultDTO = new ResultDTO();
//        Object seresult = new ArrayList();
//        String Expen_Form_select = "";
//        Expen_Form_select+="SELECT e.expen_form_id,e.expen_form_number,e.expen_form_type,e.expen_form_desc,e.expen_form_bignumber,e.expen_form_date,e.member_id,a.attachment_id,a.attachment_addr,e.isverify FROM expen_form AS e,attachment AS a WHERE e.member_id ='" + member_id+"' and a.attachment_id=e.attachment_id";
//        seresult=getReportingTo(Expen_Form_select);
//        ArrayList<ExpenFormShowDTO> edos=new ArrayList<ExpenFormShowDTO>();

    @Override
    public ResultDTO addExpenformInfo(ExpenFormPullDTO xpenFormPullDTO) {
        ResultDTO resultDTO = new ResultDTO();
        int resultls = 0;

        String existName = "";
        /**
         * 判断报销申请单中是否已存在此数据
         *
         * 若存在  进行更新
         *
         * 如不存在 执行add方法
         *
         */
        String expen_form_id = xpenFormPullDTO.getExpen_form_id();
        if (expen_form_id != null && !expen_form_id.equals("")) {

            String sql = "select count(1) from expen_form where expen_form_id='" + xpenFormPullDTO.getExpen_form_id() + "'";

            existName = getReportingToOne(sql);

            if (!existName.equals("0")) {
                return updateExpenformInfo(xpenFormPullDTO);
            }
        }

        String Expen_Form_insert = "";
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        String id = "";
        //插入图片到附件
        if (xpenFormPullDTO.getFilestream() != null && !xpenFormPullDTO.getFilestream().equals("")) {
            id = getImage(xpenFormPullDTO);
        }

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        Expen_Form_insert += "insert into expen_form(expen_form_id,expen_form_number,expen_form_type,expen_form_desc,expen_form_bignumber,expen_form_date," +
                "member_id,attachment_id,isverify,fail_resource,expen_form_name,expen_from_firsttype_id,form_code_number,form_code) VALUES('" + uuid + "','" + xpenFormPullDTO.getExpen_form_number() + "','" + xpenFormPullDTO.getExpen_form_type() + "','" +
                xpenFormPullDTO.getExpen_form_desc() + "','" + xpenFormPullDTO.getExpen_form_bignumber() + "','" +
                time + "','" + xpenFormPullDTO.getMember_id() + "','" + id + "','" + 0 + "','" + " " + "','" + xpenFormPullDTO.getExpen_form_name() + "','" + xpenFormPullDTO.getExpen_from_firsttype_id() + "','" + xpenFormPullDTO.getForm_code_number() + "','" + xpenFormPullDTO.getForm_code() + "')";
        //插入业务表
        resultls = addReportingTo(Expen_Form_insert);
        System.out.print(Expen_Form_insert);
        if (resultls != 0) {
            resultDTO.setStr(uuid);
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);

            /**
             * JSON解析集合
             */
            if (null != xpenFormPullDTO.getExpentype() && !"".equals(xpenFormPullDTO.getExpentype())) {
                String advancetype = xpenFormPullDTO.getExpentype();
                try {
                    JSONArray array = new JSONArray(advancetype);
                    if (array != null && array.length() > 0) {
                        List<ExpenFormPullDTO> result = new ArrayList<ExpenFormPullDTO>();
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject jsonObject = array.getJSONObject(i);
                            String ids = jsonObject.getString("id");
                            String name = jsonObject.getString("name");
                            String price = jsonObject.getString("price");
                            String advance_starttime = jsonObject.getString("starttime");
                            String advance_endtime = jsonObject.getString("endtime");
                            String descript = jsonObject.getString("descript");
                            ExpenformnamePullDTO expenformnamePullDTO = new ExpenformnamePullDTO();
                            expenformnamePullDTO.setExpen_form_id(uuid);
                            expenformnamePullDTO.setAdvancetypess_id(ids);
                            expenformnamePullDTO.setAdvancetypess_name(name);
                            expenformnamePullDTO.setAdvancetypess_price(price);
                            expenformnamePullDTO.setAdvance_starttime(advance_starttime);
                            expenformnamePullDTO.setAdvance_endtime(advance_endtime);
                            expenformnamePullDTO.setAdvance_desciption(descript);
                            expenformnameResource.addExpenformNameInfo(expenformnamePullDTO);
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
            //操作日志存储
            OperatorLogPullDTO operatorlogPullDTO = new OperatorLogPullDTO();
            operatorlogPullDTO.setWork_form_id(uuid);
            operatorlogPullDTO.setOperator_log_type("报销单申请");
            operatorlogPullDTO.setOperator_log_title(xpenFormPullDTO.getExpen_form_name());
            operatorlogPullDTO.setOperator_log_content(xpenFormPullDTO.getExpen_form_desc());
            operatorlogPullDTO.setMember_id(xpenFormPullDTO.getMember_id());
            operatorLogResource.addOperatorLogInfo(operatorlogPullDTO);
            //流程上报

            WorkFlowPullDTO workFlowPullDTO = new WorkFlowPullDTO();
            workFlowPullDTO.setWork_flow_formid(uuid);
            workFlowPullDTO.setWork_flow_type("expen_form");
            workFlowPullDTO.setWork_flow_desc("报销单申请请求审批");
            workFlowPullDTO.setWork_flow_date(time);
            workFlowPullDTO.setWork_flow_order("0");
            workFlowPullDTO.setWork_flow_createid(xpenFormPullDTO.getMember_id());
            workFlowPullDTO.setWork_flow_approval(xpenFormPullDTO.getReport_people());
            workFlowPullDTO.setWork_flow_approval_organization_id(xpenFormPullDTO.getWork_flow_approval_organization_id());
            workFlowPullDTO.setFlow_path_id(xpenFormPullDTO.getFlow_path_id());
            workFlowPullDTO.setWork_flow_suggestion("");
            workFlowResource.addWorkFlowInfo(workFlowPullDTO);

        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    /**
     * 修改报销单申请信息
     *
     * @param xpenFormPullDTO
     * @return
     */
    @Override
    public ResultDTO updateExpenformInfo(ExpenFormPullDTO xpenFormPullDTO) {

        ResultDTO resultDTO = new ResultDTO();

        String QUERY = "";

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


        /**
         * JSON解析集合
         */
        if (null != xpenFormPullDTO.getExpentype() && !"".equals(xpenFormPullDTO.getExpentype())) {
            String advancetype = xpenFormPullDTO.getExpentype();
            try {
                JSONArray array = new JSONArray(advancetype);
                if (array != null && array.length() > 0) {
                    ExpenformnamePullDTO expenformnamePullDTOdelete=new ExpenformnamePullDTO();
                    expenformnamePullDTOdelete.setExpen_form_id(xpenFormPullDTO.getExpen_form_id());
                    expenformnameResource.deleteExpenformNameInfo(expenformnamePullDTOdelete);

                    List<ExpenFormPullDTO> result = new ArrayList<ExpenFormPullDTO>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject jsonObject = array.getJSONObject(i);
                        String ids = jsonObject.getString("id");
                        String name = jsonObject.getString("name");
                        String price = jsonObject.getString("price");
                        String advance_starttime = jsonObject.getString("starttime");
                        String advance_endtime = jsonObject.getString("endtime");
                        String descript = jsonObject.getString("descript");
                        ExpenformnamePullDTO expenformnamePullDTO = new ExpenformnamePullDTO();
                        expenformnamePullDTO.setExpen_form_id(xpenFormPullDTO.getExpen_form_id());
                        expenformnamePullDTO.setAdvancetypess_id(ids);
                        expenformnamePullDTO.setAdvancetypess_name(name);
                        expenformnamePullDTO.setAdvancetypess_price(price);
                        expenformnamePullDTO.setAdvance_starttime(advance_starttime);
                        expenformnamePullDTO.setAdvance_endtime(advance_endtime);
                        expenformnamePullDTO.setAdvance_desciption(descript);
                        expenformnameResource.addExpenformNameInfo(expenformnamePullDTO);
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        //更新sql语句
        QUERY += "update expen_form set expen_form_date ='" + df.format(new Date()) + "'";
        if (null != xpenFormPullDTO.getExpen_form_number() && !"".equals(xpenFormPullDTO.getExpen_form_number())) {
            QUERY += ",expen_form_number = '" + xpenFormPullDTO.getExpen_form_number() + "'";
        }

        if (null != xpenFormPullDTO.getExpen_form_type() && !"".equals(xpenFormPullDTO.getExpen_form_type())) {
            QUERY += ",expen_form_type ='" + xpenFormPullDTO.getExpen_form_type() + "'";
        }

        if (null != xpenFormPullDTO.getExpen_form_desc() && !"".equals(xpenFormPullDTO.getExpen_form_desc())) {
            QUERY += ",expen_form_desc ='" + xpenFormPullDTO.getExpen_form_desc() + "'";
        }

        if (null != xpenFormPullDTO.getExpen_form_bignumber() && !"".equals(xpenFormPullDTO.getExpen_form_bignumber())) {
            QUERY += ",expen_form_bignumber ='" + xpenFormPullDTO.getExpen_form_bignumber() + "'";
        }

        if (null != xpenFormPullDTO.getFail_resource() && !"".equals(xpenFormPullDTO.getFail_resource())) {
            QUERY += ",fail_resource ='" + xpenFormPullDTO.getFail_resource() + "'";
        }
        if (null != xpenFormPullDTO.getExpen_form_name() && !"".equals(xpenFormPullDTO.getExpen_form_name())) {
            QUERY += ",expen_form_name ='" + xpenFormPullDTO.getExpen_form_name() + "'";
        }
        if (null != xpenFormPullDTO.getIsverify() && !"".equals(xpenFormPullDTO.getIsverify())) {
            QUERY += ",isverify ='" + xpenFormPullDTO.getIsverify() + "'";
        }
        if (null != xpenFormPullDTO.getAttachment_id() && !"".equals(xpenFormPullDTO.getAttachment_id()) && null != xpenFormPullDTO.getFilestream() && !"".equals(xpenFormPullDTO.getFilestream())) {
            //调用修改文件方法
            updateImage(xpenFormPullDTO);
        } else if (null != xpenFormPullDTO.getFilestream() && !"".equals(xpenFormPullDTO.getFilestream())) {
            String uid = getImage(xpenFormPullDTO);
            QUERY += ",attachment_id='" + uid + "'";

        }
//
//        if(null!=dataDTO.yuliu&&!"".equals(dataDTO.yuliu)){
//            QUERY += ",yuliu ='" + dataDTO.getYuliu() + "'";
//        }
//
        QUERY += " where expen_form_id = '" + xpenFormPullDTO.getExpen_form_id() + "'";


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {
            WorkFlowPullDTO workFlowPullDTO = new WorkFlowPullDTO();
            workFlowPullDTO.setWork_flow_formid(xpenFormPullDTO.getExpen_form_id());
            workFlowPullDTO.setWork_flow_approval(xpenFormPullDTO.getReport_people());
            workFlowPullDTO.setWork_flow_approval_organization_id(xpenFormPullDTO.getWork_flow_approval_organization_id());
            workFlowPullDTO.setIsverify("0");
            workFlowResource.updateWorkFlowInfo(workFlowPullDTO);

            resultDTO.setStr(xpenFormPullDTO.getExpen_form_id());
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }


    /**
     * 删除报销申请信息
     *
     * @param xpenFormPullDTO
     * @return
     */
    @Override
    public ResultDTO deleteExpenformInfo(ExpenFormPullDTO xpenFormPullDTO) {

        //删除之前查询一下该表是否有记录
        String sql = "select count(1) from expen_form where expen_form_id='" + xpenFormPullDTO.getExpen_form_id() + "'";

        String existName = getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();

        if (!existName.equals("0")) {

            String QUERY = "";
            QUERY += "delete from expen_form where expen_form_id='" + xpenFormPullDTO.getExpen_form_id() + "'";

            result = this.addReportingTo(QUERY);
        }

        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);

            ExpenformnamePullDTO expenformnamePullDTO = new ExpenformnamePullDTO();
            expenformnamePullDTO.setExpen_form_id(xpenFormPullDTO.getExpen_form_id());
            expenformnameResource.deleteExpenformNameInfo(expenformnamePullDTO);

            WorkFlowPullDTO workFlowPullDTO = new WorkFlowPullDTO();
            workFlowPullDTO.setWork_flow_formid(xpenFormPullDTO.getExpen_form_id());
            workFlowResource.deleteWorkFlowInfo(workFlowPullDTO);

        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }

    /**
     * 保存图片方法
     *
     * @param
     * @return
     */
    public String getImage(ExpenFormPullDTO xpenFormPullDTO) {
        ResultDTO resultDTO = new ResultDTO();
        // TODO Auto-generated method stub
        String enpenform_image = "";
        //获取配置图片地址
        String[] image_path = this.getImage_path();
        String image_pathAddr = image_path[1];//服务器项目目录地址
        String image_pathSerAddr = image_path[2];//图片访问地址
        String imgDirct = image_pathAddr + "/expenform";
        File f = new File(imgDirct);
        if (!f.exists()) {
            f.mkdirs();
            f.setWritable(true, false);
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String imgPath = imgDirct + "/" + xpenFormPullDTO.getFile_type();


        //base64字符串保存为图片
        boolean headImg = FileProcess.GenerateImage(xpenFormPullDTO.getFilestream(), imgPath);
        FileProcess.newlog("执行头像图片储存方法，储存照片地址：" + imgPath + " 方法返回：" + headImg);
        int resultls = 0;
        int mresult = 0;

        String imgurl = "/expenform/" + xpenFormPullDTO.getFile_type();

        //ArrayList<HeadPortaitDTO> result = new ArrayList<HeadPortaitDTO>();

        if (headImg) {
            //插入头像表
            enpenform_image += "insert into attachment(attachment_id,attachment_addr,head_portrait_time,file_type,file_name) values ('" + uuid + "','" + imgurl + "','" + df.format(new Date()) + "','" + xpenFormPullDTO.getFile_type() + "','" + xpenFormPullDTO.getFile_name() + "')";
            // enpenform_image += "insert into file(file_id,file_addr,file_time,filegui_id,file_name) values ('" + uuid + "','" +imgurl+ "','" +df.format(new Date()) + "','" + dataDTO.getFilegui_id() + "','" + dataDTO.getFiletype() + "')";
            //执行sql
            resultls = addReportingTo(enpenform_image);
        }
        if (resultls != 0) {
            return uuid;
        }

        return null;
    }
//     public String getImage(String image) {
    // TODO Auto-generated method stub
//        String enpenform_image = "";
//        //获取配置图片地址
//        String[] image_path = this.getImage_path();
//        String image_pathAddr = image_path[1];//服务器项目目录地址
//        String image_pathSerAddr = image_path[2];//图片访问地址
//        String imgDirct = image_pathAddr + "/expenform";
//        File f = new File(imgDirct);
//        if (!f.exists()) {
//            f.mkdirs();
//            f.setWritable(true, false);
//        }
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
//
//        String imgPath = imgDirct + "/" + uuid + ".jpg";
//
//        //base64字符串保存为图片
//        boolean headImg = FileProcess.GenerateImage(image, imgPath);
//        FileProcess.newlog("执行头像图片储存方法，储存照片地址：" + imgPath + " 方法返回：" + headImg);
//        int resultls = 0;
//        int mresult = 0;
//
//        String imgurl = image_pathSerAddr + "/expenform/" + uuid + ".jpg";
//
//        //ArrayList<HeadPortaitDTO> result = new ArrayList<HeadPortaitDTO>();
//
//        if (headImg) {
//            //插入头像表
//            enpenform_image += "insert into attachment(attachment_id,attachment_addr,head_portrait_time) values ('" + uuid + "','" + imgurl + "','" + df.format(new Date()) + "')";
//        }
//
//        //执行sql
//        resultls = addReportingTo(enpenform_image);
//
//        if (resultls != 0) {
//            return uuid;
//        }
//        return null;
    //   }

    /**
     * 修改图片方法
     *
     * @param
     * @return
     */
    public String updateImage(ExpenFormPullDTO xpenFormPullDTO) {
        // TODO Auto-generated method stub
        String enpenform_image = "";
        //获取配置图片地址
        String[] image_path = this.getImage_path();
        String image_pathAddr = image_path[1];//服务器项目目录地址
        String image_pathSerAddr = image_path[2];//图片访问地址
        String imgDirct = image_pathAddr + "/expenform";
        File f = new File(imgDirct);
        if (!f.exists()) {
            f.mkdirs();
            f.setWritable(true, false);
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String imgPath = imgDirct + "/" + xpenFormPullDTO.getFile_type();

        //base64字符串保存为图片
        boolean headImg = FileProcess.GenerateImage(xpenFormPullDTO.getFilestream(), imgPath);
        FileProcess.newlog("执行头像图片储存方法，储存照片地址：" + imgPath + " 方法返回：" + headImg);
        int resultls = 0;
        int mresult = 0;

        String imgurl = "/expenform/" + xpenFormPullDTO.getFile_type();

        //ArrayList<HeadPortaitDTO> result = new ArrayList<HeadPortaitDTO>();

        if (headImg) {
            //修改头像表
            enpenform_image += "update attachment set attachment_addr='" + imgurl + "',head_portrait_time = '" + df.format(new Date()) + "',file_type = '" + xpenFormPullDTO.getFile_type() + "',file_name = '" + xpenFormPullDTO.getFile_name() + "' where attachment_id='" + xpenFormPullDTO.getAttachment_id() + "'";
            // enpenform_image += "insert into attachment(attachment_id,attachment_addr,head_portrait_time) values ('" + uuid + "','" + imgurl + "','" + df.format(new Date()) + "')";
            //执行sql
            resultls = addReportingTo(enpenform_image);

        }
        if (resultls != 0) {
            return uuid;
        }

        return null;
    }
}
